package com.ls.fw.data.hbase.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.MD5Hash;

public class RowUtils {

    public static final String MD5="MD5";

    private static final Random random = new Random();   
    /**
     * 采用加密算法加密字符串数据
     * @param str   需要加密的数据
     * @param algorithm 采用的加密算法
     * @return 字节数据
     */
    public static byte[] md5(String str) {
        return md5(str.getBytes());
    }
    
    public static byte[] md5(byte[] str) {
        // 加密之后所得字节数组
        byte[] bytes = null;
        try {
            // 获取MD5算法实例 得到一个md5的消息摘要
            MessageDigest md = MessageDigest.getInstance(MD5);
            //添加要进行计算摘要的信息
            md.update(str);
            //得到该摘要
            bytes = md.digest();
        } catch (NoSuchAlgorithmException e) {
            System.out.println("加密算法: "+ MD5 +" 不存在: ");
        }
        return null==bytes?null:bytes;
    }
    
    public static byte[] get(Long id){
    	byte [] rowkey = Bytes.add(MD5Hash.getMD5AsHex(Bytes.toBytes(id))  
    			.substring(0, 8).getBytes(),Bytes.toBytes(id));  
    	return rowkey;
    }

    public static  String get(String table,String id) {
    	return "";
    }
    
    public static  String get(String table,long date,Long id) {
    	String md5_content = table+date+id;
		try {
			MessageDigest messageDigest = MessageDigest.getInstance("MD5");
			messageDigest.reset();
			messageDigest.update((table).getBytes());
			byte[] bytes = messageDigest.digest();
			md5_content = new String(Hex.encodeHex(bytes));
		} catch (NoSuchAlgorithmException e1) {
			e1.printStackTrace();
		}
		// turn right md5
		String right_md5_id = Integer.toHexString(Integer.parseInt(md5_content.substring(0, 7), 16) >> 1);
		while (right_md5_id.length() < 7) {
			right_md5_id = "0" + right_md5_id;
		}
		if(id==null){
			return right_md5_id+"_" +(Long.MAX_VALUE-date);
		}else{
			return right_md5_id+"_" +(Long.MAX_VALUE-date)+"_"+id;
		}
    }
    
    
    public static  String get(String table,long date,String id) {
		String md5_content = id;
		try {
			MessageDigest messageDigest = MessageDigest.getInstance("MD5");
			messageDigest.reset();
			messageDigest.update((table).getBytes());
			byte[] bytes = messageDigest.digest();
			md5_content = new String(Hex.encodeHex(bytes));
		} catch (NoSuchAlgorithmException e1) {
			e1.printStackTrace();
		}
		// turn right md5
		String right_md5_id = Integer.toHexString(Integer.parseInt(md5_content.substring(0, 7), 16) >> 1);
		while (right_md5_id.length() < 7) {
			right_md5_id = "0" + right_md5_id;
		}
		if(StringUtils.isBlank(id)){
			return right_md5_id+"_" +(Long.MAX_VALUE-date);
		}else{
			return right_md5_id+"_" +(Long.MAX_VALUE-date)+"_"+id;
		}
	}

    public static  String get2(String table,long date,String id) {
    	long date1 =date;//+ random.nextInt(1000);  
          byte[] lowT = Bytes.copy(Bytes.toBytes(date1), 4, 4);  
          byte[] lowU = Bytes.copy(Bytes.toBytes(table), 0, 4);  
          return Bytes.toString(Bytes.add(MD5Hash.getMD5AsHex(Bytes.add(lowU, lowT)).substring(0, 8).getBytes(),  
                  Bytes.toBytes(random.nextInt(1000)+id)));  
	}
    public static void main(String[] args) {
    	int i = 10000;
    	Set<String> set = new HashSet<String>();
    	while(i>0){
    		System.out.println(Bytes.toBytes(i).length);
    		String key = get2("dadfsadf",new Date().getTime() ,i+"");
    		System.out.println(key);
    		String key1 = get2("dadfsadf",0 ,"");
    		System.out.println(key1);
    		if(set.contains(key)){
    			break;
    		}
    		set.add(key);
    		i--;
    	}
	}
}
